/*
 * @copyright fxsen <br/>
 * @date 2022/3/30 17:25 <br/>
 * @email <a href="mailto:fxsen@foxmail.com">
 * @author fxsen
 */
package com.fxsen.commons.core.base.result;

/**
 * 响应通用结果 多表CUD操作必须放到统一事务（同一个service接口），并且返回ResultJson
 * @author fxsen
 * @date 2022/3/30 17:25
 * @email <a href="mailto:fxsen@foxmail.com">
 */
public class ResultJson {
    private boolean success = true;

    private String msg = "操作成功！";
    private Object obj = null;
    public static final ResultJson OK = new ResultJson(true);
    public static final ResultJson ERROR = new ResultJson(false);
    public static final ResultJson ERROR_PARAM = new ResultJson(false, "未传请求参数！", null);
    public static final ResultJson ERROR_SAVE = new ResultJson(false, "保存失败！", null);
    public static final ResultJson ERROR_DELETE = new ResultJson(false, "删除失败！", null);
    public static final ResultJson ERROR_UPDATE = new ResultJson(false, "更新失败！", null);
    public static final ResultJson ERROR_ERRO = new ResultJson(false, "接口异常！", null);

    public ResultJson() {
        super();
    }
    public ResultJson(boolean success) {
        super();
        this.success = success;
    }
    public ResultJson(boolean success, String msg){
        this.success = success;
        this.msg = msg;
    }

    public ResultJson(boolean success, String msg, Object obj) {
        super();
        this.success = success;
        this.msg = msg;
        this.obj = obj;
    }

    public boolean isSuccess() {
        return success;
    }

    public ResultJson setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public ResultJson setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getObj() {
        return obj;
    }

    public ResultJson setObj(Object obj) {
        this.obj = obj;
        return this;
    }
    public static ResultJson success() {
        return new ResultJson().setSuccess(true);
    }
    public static <U> ResultJson success(U u) {
        return new ResultJson().setSuccess(true).setObj(u);
    }
    public static <U> ResultJson success(String message, U u) {
        return new ResultJson().setSuccess(true).setObj(u).setMsg(message);
    }

    public static <U> ResultJson message(String message) {
        return new ResultJson().setSuccess(true).setMsg(message);
    }

    public static <U> ResultJson error(String message) {
        return new ResultJson().setSuccess(false).setMsg(message);
    }
    public static <U> ResultJson error(String message, U u) {
        return new ResultJson().setSuccess(false).setObj(u).setMsg(message);
    }
}
